package com.xinke.module.system.controller.admin.oauth2;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.oauth2.*;
import com.xinke.module.system.model.entity.oauth2.OAuth2CodeDO;
import com.xinke.module.system.service.oauth2.OAuth2CodeService;

@Tag(name = "管理后台 - OAuth2 授权码")
@RestController
@RequestMapping("/system/O-auth2-code")
@Validated
public class OAuth2CodeController {

    @Resource
    private OAuth2CodeService oAuth2CodeService;

    @PostMapping("/create")
    @Operation(summary = "创建OAuth2 授权码")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-code:create')")
    public Result<Long> createOAuth2Code(@Valid @RequestBody OAuth2CodeSaveReqVO createReqVO) {
        return success(oAuth2CodeService.createOAuth2Code(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新OAuth2 授权码")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-code:update')")
    public Result<Boolean> updateOAuth2Code(@Valid @RequestBody OAuth2CodeSaveReqVO updateReqVO) {
        oAuth2CodeService.updateOAuth2Code(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除OAuth2 授权码")
    @Parameter(name = "codeId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:O-auth2-code:delete')")
    public Result<Boolean> deleteOAuth2Code(@RequestParam("codeId") Long codeId) {
        oAuth2CodeService.deleteOAuth2Code(codeId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得OAuth2 授权码")
    @Parameter(name = "codeId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-code:query')")
    public Result<OAuth2CodeRespVO> getOAuth2Code(@RequestParam("codeId") Long codeId) {
        OAuth2CodeDO oAuth2Code = oAuth2CodeService.getOAuth2Code(codeId);
        return success(BeanUtils.toBean(oAuth2Code, OAuth2CodeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得OAuth2 授权码分页")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-code:query')")
    public Result<PageResult<OAuth2CodeRespVO>> getOAuth2CodePage(@Valid OAuth2CodePageReqVO pageReqVO) {
        PageResult<OAuth2CodeDO> pageResult = oAuth2CodeService.getOAuth2CodePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OAuth2CodeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出OAuth2 授权码 Excel")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-code:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOAuth2CodeExcel(@Valid OAuth2CodePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OAuth2CodeDO> list = oAuth2CodeService.getOAuth2CodePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "OAuth2 授权码.xls", "数据", OAuth2CodeRespVO.class,
                        BeanUtils.toBean(list, OAuth2CodeRespVO.class));
    }

}